<?php
namespace app\admin\controller;
use think\Db;
use think\Paginator;

class Tongji extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $month = date('Y-m',strtotime('last month'));
            $map = [
                'record_status' => 2,
                'record_type' => ['<>',2],
                'record_pay_month' => $month
            ];
            if(isset($key)&&$key!="")
            {
                $map['shop_contact|shop_name'] = ['like',"%" . $key . "%"];
            }
            if(isset($binds)&&$binds!="")
            {
                if($binds == 1) $map['ss_shop_id'] = null;
                if($binds == 2) $map['ss_shop_id'] = ['>',0];
            }
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $map['record_pay_month'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $map['record_pay_month'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $map['record_pay_month'] = ['between',[$start,$end]];
            }
            if(session ('area')){
                $map['shop_qu_id'] = ['in',session('area')];
            }
            if(isset($quid)&&$quid!="")
            {
                $map['shop_qu_id'] = $quid;
            }
            if(isset($dabiao)&&$dabiao!="")
            {
                $map['record_pay_month_done'] = $dabiao;
            }
            if(isset($butie)&&$butie!="")
            {
                $map['shop_butie'] = $butie;
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('record r')
                ->join("u_user u",'r.record_user_id = u.user_id','left')
                ->join("shop s",'r.record_shop_id = s.shop_id','left')
                ->join("up_shop_ercode sc",'r.record_shop_id = sc.ss_shop_id','left')
                ->where($map)
                ->group('record_shop_id,record_pay_month')
                ->count();
            $lists = Db::name('record r')
                ->join("u_user u",'r.record_user_id = u.user_id','left')
                ->join("shop s",'r.record_shop_id = s.shop_id','left')
                ->join("up_shop_ercode sc",'r.record_shop_id = sc.ss_shop_id','left')
                ->where($map)
                ->group('record_shop_id,record_pay_month')
                ->field('r.*,u.user_username,shop_qu_id,shop_id,shop_name,shop_butie,shop_contact,sc.ss_shop_id,sc.ss_bindtime')
                ->page($Nowpage,$limits)
                ->order('record_add_time DESC')
                ->select ();
            foreach ($lists as $k => $v){
                $monthMoney = Db::name('record')
                    ->where('record_status',2)
                    ->where('record_type','<>',2)
                    ->where('record_pay_month',$v['record_pay_month'])
                    ->where('record_shop_id',$v['record_shop_id'])
                    ->field('count(*) nums,sum(record_money) money')
                    ->find();
                $lists[$k]['nums'] = $monthMoney['nums']; //当前月笔数
                $lists[$k]['money'] = $monthMoney['money']; //当前月钱数
                $daili = Db::name('admin')->where('area',$v['shop_qu_id'])
                    ->field('sign_start,username,admin_days')->find();
                $dailiTime = strtotime($daili['sign_start']);
                $ymd90 = date('Y-m-d',$dailiTime+3600*24*$daili['admin_days']);
                $cha = strtotime($ymd90) - strtotime(date('Y-m-d'));
                if($cha <= 0){
                    $lists[$k]['days'] = "<span style='color: red;'>已到期</span>";
                }else{
                    $lists[$k]['days'] = "<span style='color: mediumseagreen;'>" . $cha/86400 . "</span>";
                }
                $lists[$k]['dailiname'] = $daili['username'];
            }
            $allMoney = Db::name('record r')
                ->join("u_user u",'r.record_user_id = u.user_id','left')
                ->join("shop s",'r.record_shop_id = s.shop_id','left')
                ->join("up_shop_ercode sc",'r.record_shop_id = sc.ss_shop_id','left')
                ->where($map)
                ->sum('record_money');
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists,'allmoney'=>$allMoney]);
        }
        $where = [
            'level' => 3
        ];
        if(session ('area')){
            $where['district_id'] = ['in',session('area')];
        }else{
            $city_area   = DB::name('shop')->where(['shop_status'=>1])->group('shop_qu_id')->column('shop_qu_id');
            $where['district_id'] = ['in',$city_area];
        }
        $quuus = Db::table('up_area')->where($where)->field('district_id,district')->select();
        $this->assign('quuus',$quuus);
        $month = date('Y-m',strtotime('last month'));
        $this->assign('month',$month);
        return $this->fetch ('tongji/index');
    }

    /**
     * 补贴
     * @return \think\response\Json
     */
    public function butieDone(){
        $sid = input('id');
        $res = Db::table('up_shop')->where('shop_id', $sid)->setField('shop_butie', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    public function recordList(){
        extract (input());
        if(request()->isAjax ()){
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['record_number'] = ['like',"%" . $key . "%"];
            }
            $map['record_type'] = ['<>',2];
			if(isset($type)&&$type!="")
            {
                $map['record_type'] = $type;
            }
            $map['record_shop_id'] = session('record_shop_id');
            $map['record_pay_month'] = session('record_pay_month');
            $map['record_status'] = 2;
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('record')
                ->where($map)
                ->count();
            $lists = Db::name('record r')
                ->join("u_user u",'r.record_user_id = u.user_id','left')
                ->join("shop s",'r.record_shop_id = s.shop_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('record_pay_time DESC')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        session('record_shop_id',$id);
        session('record_pay_month',$month);
        return $this->fetch ('tongji/record_list');
    }
}